<?php
/*********************************************
 *  CPG Dragonfly™ CMS
 *********************************************
	Copyright © since 2010 by CPG-Nuke Dev Team
	http://dragonflycms.org

	Dragonfly is released under the terms and conditions
	of the GNU GPL version 2 or any later version
*/

class Dragonfly_Forums_Forum
{
	protected
		$data = array(
			'forum_id'           => 0,
			'cat_id'             => 0,
			'parent_id'          => 0,
			'forum_name'         => '',
			'forum_desc'         => '',
			'forum_status'       => FORUM_UNLOCKED,
			'forum_order'        => 0,
			'forum_posts'        => 0,
			'forum_topics'       => 0,
			'forum_last_post_id' => 0,
			'forum_type'         => 0,
			'forum_link'         => '',

			'auth_view'          => AUTH_ALL,
			'auth_read'          => AUTH_ALL,
			'auth_post'          => AUTH_ALL,
			'auth_reply'         => AUTH_ALL,
			'auth_edit'          => AUTH_REG,
			'auth_delete'        => AUTH_REG,
			'auth_sticky'        => AUTH_MOD,
			'auth_announce'      => AUTH_MOD,
			'auth_vote'          => AUTH_REG,
			'auth_pollcreate'    => AUTH_REG,
			'auth_attachments'   => AUTH_REG,
			'auth_download'      => AUTH_REG,

			'archive_enable'     => false,
			'archive_freq'       => 1, // per N days
			'archive_days'       => 7, // older then N days
			'archive_next'       => 0,
			'archive_topics'     => 0,
			'archive_posts'      => 0,

			'prune_enable'       => false,
			'prune_freq'         => 1, // per N days
			'prune_days'         => 7, // older then N days
			'prune_next'         => 0,
		);

	function __construct($forum_id=0)
	{
		$forum_id = intval($forum_id);
		if ($forum_id)
		{
			global $db;
			$data = $db->uFetchAssoc('SELECT * FROM '. FORUMS_TABLE. ' WHERE forum_id = '. $forum_id);
			foreach ($data as $k => $v) { $this->__set($k, $v); }
		}
	}

	function __get($k)
	{
		if (!array_key_exists($k, $this->data)) { $k = 'forum_'.$k; }
		if (array_key_exists($k, $this->data))  { return $this->data[$k]; }
		trigger_error("Property '{$k}' does not exist");
	}

	function __set($k, $v)
	{
		if (!array_key_exists($k, $this->data)) { $k = 'forum_'.$k; }
		if (array_key_exists($k, $this->data)) {
			if (!is_null($v)) {
				if (is_int($this->data[$k])) {
					$this->data[$k] = intval($v);
				} else if (is_bool($this->data[$k])) {
					$this->data[$k] = !!$v;
				} else {
					$this->data[$k] = trim($v);
				}
			}
		} else {
			trigger_error("Property '{$k}' does not exist");
		}
	}

	public function getTypeOptions()
	{
		return array(
			array(
				'name' => 'Normal Forum',
				'value' => 0,
				'current' => (0 == $this->type),
			),
			array(
				'name' => 'Parent Forum',
				'value' => 1,
				'current' => (1 == $this->type),
			),
			array(
				'name' => 'Local URL',
				'value' => 2,
				'current' => (2 == $this->type),
			),
			array(
				'name' => 'Remote URL',
				'value' => 3,
				'current' => (3 == $this->type),
			),
		);
	}

	public function save()
	{
		if (!$this->data['cat_id'] || !$this->data['forum_name'])
		{
			return false;
		}

		global $db;

		if (!$this->data['forum_order'])
		{
			list($next_order) = $db->uFetchRow('SELECT MAX(forum_order) FROM '. FORUMS_TABLE. ' WHERE cat_id = '. $this->data['cat_id']);
			$this->data['forum_order'] = $next_order + 10;
		}

		$this->data['archive_freq'] = max(1,$this->data['archive_freq']);
		$this->data['archive_days'] = max(1,$this->data['archive_days']);
		$this->data['prune_freq']   = max(1,$this->data['prune_freq']);
		$this->data['prune_days']   = max(1,$this->data['prune_days']);

		$data = $this->data;
		$data['forum_name'] = $db->quote($data['forum_name']);
		$data['forum_desc'] = $db->quote($data['forum_desc']);
		$data['forum_link'] = $db->quote($data['forum_link']);
		$data['archive_enable'] = intval($data['archive_enable']);
		$data['prune_enable']   = intval($data['prune_enable']);

		if (!$data['forum_id'])
		{
			$db->query('INSERT INTO '. FORUMS_TABLE. ' ('.implode(',',array_keys($data)).') VALUES ('.implode(',',$data).')');
			$this->data['forum_id'] = $db->insert_id('forum_id');
		}
		else
		{
			$db->query('UPDATE '. FORUMS_TABLE. " SET
				forum_name = ". $data['forum_name']. ",
				cat_id = ". $data['cat_id']. ",
				parent_id = ". $data['parent_id']. ",
				forum_desc = ". $data['forum_desc']. ",
				forum_status = ". $data['forum_status']. ",
				forum_type = ". $data['forum_type']. ",
				forum_link = ". $data['forum_link']. ",
				archive_enable = ". $data['archive_enable']. ",
				archive_freq = ". $data['archive_freq']. ",
				archive_days = ". $data['archive_days']. ",
				prune_enable = ". $data['prune_enable']. ",
				prune_freq = ". $data['prune_freq']. ",
				prune_days = ". $data['prune_days']. "
			WHERE forum_id = ". $this->data['forum_id']);
		}

		return true;
	}

	//
	// This function will call the archive function with the necessary info.
	//
	public function autoArchive()
	{
		if ($this->archive_enable && $this->archive_next < time() && $this->archive_freq && $this->archive_days)
		{
			$this->archive(time() - ($this->archive_days * 86400));
			$this->sync();
			global $db;
			$db->exec("UPDATE ".FORUMS_TABLE."
			SET archive_next = ".time() + ($this->archive_freq * 86400)."
			WHERE forum_id = {$this->data['forum_id']}");
		}
	}

	public function archive($archive_date, $archive_all = false)
	{
		require_once('includes/phpBB/archive.php');
		return archive($this->data['forum_id'], $archive_date, $archive_all);
	}

	//
	// This function will call the prune function with the necessary info.
	//
	public function autoPrune()
	{
		if ($this->prune_enable && $this->prune_next < time() && $this->prune_freq && $this->prune_days)
		{
			$this->prune(time() - ($this->prune_days * 86400));
			$this->sync();
			global $db;
			$db->exec("UPDATE ".FORUMS_TABLE."
			SET prune_next = ".time() + ($this->prune_freq * 86400)."
			WHERE forum_id = {$this->data['forum_id']}");
		}
	}

	public function prune($prune_date, $prune_all = false)
	{
		require_once('includes/phpBB/prune.php');
		return prune($this->data['forum_id'], $prune_date, $prune_all);
	}

	public function sync()
	{
		global $db;

		$row = $db->uFetchRow("SELECT
			MAX(post_id),
			COUNT(post_id)
		FROM " . POSTS_TABLE . "
		WHERE forum_id = {$this->data['forum_id']}");

		if ($row) {
			$last_post = intval($row[0]);
			$total_posts = intval($row[1]);
		} else {
			$last_post = 0;
			$total_posts = 0;
		}

		$row = $db->uFetchRow("SELECT COUNT(topic_id) FROM " . TOPICS_TABLE . "
		WHERE forum_id = {$this->data['forum_id']}
		  AND topic_archive_flag = 0");
		$total_topics = $row ? intval($row[0]) : 0;

		$db->exec("UPDATE " . FORUMS_TABLE . " SET
			forum_last_post_id = {$last_post},
			forum_posts = {$total_posts},
			forum_topics = {$total_topics}
		WHERE forum_id = {$this->data['forum_id']}");
	}

}
